package com.dchh.android.suverynew.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.bean.ImageInfoBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * @author zhoudan
 * @date 2016年1月18日
 */
public class PhotoGridAdapter extends BaseAdapter {

    private Activity activity;
    private List<ImageInfoBean> list;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    private ImageOperate operate;

    private boolean flag = false;

    public PhotoGridAdapter(Activity activity, List<ImageInfoBean> list) {
        this.activity = activity;
        this.list = list;

        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(activity));
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_stub)
                .showImageForEmptyUri(R.mipmap.ic_empty)
                .showImageOnFail(R.mipmap.ic_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    public void setDeleteFlag(boolean flag) {
        this.flag = flag;
    }

    public void setImageOperate(ImageOperate operate) {
        this.operate = operate;
    }

    public void clearMemory() {
        imageLoader.clearMemoryCache();
        imageLoader.clearDiskCache();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ImageInfoBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;
        if (row == null) {
            holder = new ViewHolder();

            row = LayoutInflater.from(activity).inflate(R.layout.photo_zw_grid_item, null);
            holder.del = (LinearLayout) row.findViewById(R.id.att_del);
            holder.img = (ImageView) row.findViewById(R.id.att_img);
            holder.name = (TextView) row.findViewById(R.id.att_name);

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        ImageInfoBean att = getItem(position);

        final String path = att.getPath();
        String name = att.getImageMsg();

        if (holder.img != null) {
            imageLoader.displayImage("file://" + path, holder.img, options);

        }
        holder.name.setText(name);

        if (flag) {
            holder.del.setVisibility(View.VISIBLE);
        } else {
            holder.del.setVisibility(View.GONE);
        }

        holder.del.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (operate != null) {
                    operate.OnDelete(position);
                }
            }
        });

        holder.img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (operate != null) {
                    operate.OnShow(position, v);
                }
            }
        });

        return row;
    }


    public final class ViewHolder {
        public LinearLayout del;
        public ImageView img;
        public TextView name;
    }

    public interface ImageOperate {
        public void OnShow(int position, View view);

        public void OnDelete(int position);
    }
}
